package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import org.springframework.context.ApplicationContext;
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXALopMonHoctblQLDTTXALopMonHocHocVienInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private int lopMonHocId; 
    public void setLopMonHocId(int lopMonHocId) {
       this.lopMonHocId = lopMonHocId;
 }

    public int getLopMonHocId() {
       return lopMonHocId;
 }
    
    private int status; 
    public void setStatus(int status) {
       this.status = status;
 }

    public int getStatus() {
       return status;
 }

    private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService; 

    private double diemCK; 
    public void setDiemCK(double diemCK) {
       this.diemCK = diemCK;
 }

    public double getDiemCK() {
       return diemCK;
 }

    private double diemGK; 
    public void setDiemGK(double diemGK) {
       this.diemGK = diemGK;
 }

    public double getDiemGK() {
       return diemGK;
 }

    private double diemTH; 
    public void setDiemTH(double diemTH) {
       this.diemTH = diemTH;
 }

    public double getDiemTH() {
       return diemTH;
 }

    private double tongdiem; 
    public void setTongdiem(double tongdiem) {
       this.tongdiem = tongdiem;
 }

    public double getTongdiem() {
       return tongdiem;
 }

    private int trangThai; 
    public void setTrangThai(int trangThai) {
       this.trangThai = trangThai;
 }

    public int getTrangThai() {
       return trangThai;
 }

    private TblQLDTTXALopMonHocHocVien tblQLDTTXALopMonHocHocVien; 
    public TblQLDTTXALopMonHocHocVien  getTblQLDTTXALopMonHocHocVien() {
        return tblQLDTTXALopMonHocHocVien;
 }

    private List<TblQLDTTXALopMonHocHocVien> tblQLDTTXALopMonHocHocViens; 
    public List<TblQLDTTXALopMonHocHocVien>  getTblQLDTTXALopMonHocHocViens() {
        return tblQLDTTXALopMonHocHocViens; 
      } 

    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService; 
    
    private TblQLDTTXAHocVienDangKyMonHocService tblQLDTTXAHocVienDangKyMonHocService;

	private int preInsert(Message msg, TblQLDTTXALopMonHocHocVien obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblQLDTTXALopMonHocHocVien obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc_HocVien", "them")){ 
        return "noprivilege"; 
    }
        tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient");
        
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService) context.getBean("tbl_QLDTTXA_LopMonHocClient");
        
        tblQLDTTXAHocVienDangKyMonHocService = (TblQLDTTXAHocVienDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_DangKyMonHocClient");
        
        TblQLDTTXALopMonHoc lopMonHoc = tblQLDTTXALopMonHocService.gettblQLDTTXALopMonHoc(new Long(lopMonHocId));

        msg = new Message(); 
        tblQLDTTXALopMonHocHocVien = new TblQLDTTXALopMonHocHocVien(); 
        tblQLDTTXALopMonHocHocVien.setHocVienId(tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hocVienId))); 
        tblQLDTTXALopMonHocHocVien.setLopMonHocId(lopMonHoc); 
        tblQLDTTXALopMonHocHocVien.setDiemCK(diemCK); 
        tblQLDTTXALopMonHocHocVien.setDiemGK(diemGK); 
        tblQLDTTXALopMonHocHocVien.setDiemTH(diemTH); 
        tblQLDTTXALopMonHocHocVien.setTongdiem(tongdiem); 
        tblQLDTTXALopMonHocHocVien.setTrangThai(trangThai); 
        
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("monHocId.id");
            con.setValue1(new Long(lopMonHoc.getMonHocId().getId()));
            con.setOperator("eq");
            cons.add(con);	
        }   
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("phieuDangKyId.hocKyId.id");
            con.setValue1(new Long(lopMonHoc.getHocKyId().getId()));
            con.setOperator("eq");
            cons.add(con);	
        } 
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("phieuDangKyId.hocVienId.id");
            con.setValue1(new Long(hocVienId));
            con.setOperator("eq");
            cons.add(con);	
        } 
        
        List<TblQLDTTXAHocVienDangKyMonHoc> monHocDKs = tblQLDTTXAHocVienDangKyMonHocService.findtblQLDTTXAHocVienDangKyMonHocByCondition(cons, 0, 0);
        
        if(monHocDKs != null && monHocDKs.size() > 0){
        	status = 0;
        	TblQLDTTXAHocVienDangKyMonHoc monHocDK = monHocDKs.get(0);
        	monHocDK.setDaXepLop(true);
        	
	        preInsert(msg,tblQLDTTXALopMonHocHocVien); 
	       if (msg.isPreMessage_b()) 
	       { 
	           tblQLDTTXALopMonHocHocVienService.createtblQLDTTXALopMonHocHocVien(tblQLDTTXALopMonHocHocVien);
	           tblQLDTTXAHocVienDangKyMonHocService.updatetblQLDTTXAHocVienDangKyMonHoc(monHocDK);
	           postInsert(msg, tblQLDTTXALopMonHocHocVien); 
	           if (msg.isPostMessage_b() == false) 
	           { 
	               return Action.ERROR; 
	           } 
	       }else{ 
	           return Action.ERROR; 
	       } 
        }
        else{
        	status = 1;
        }
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
        cons = new ArrayList<ConditionReport>(); 
        {
	        ConditionReport con = new ConditionReport(); 
	        con.setCol("lopMonHocId.id"); 
	        con.setValue1(new Long(lopMonHocId)); 
	        con.setOperator("eq"); 
	        cons.add(con); 
        }
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.ten"); 
            con.setValue1("asc"); 
            con.setOperator("order"); 
            cons.add(con); 
        }
      tblQLDTTXALopMonHocHocViens =       tblQLDTTXALopMonHocHocVienService.findtblQLDTTXALopMonHocHocVienByCondition(cons,0, 0); 
      pages = tblQLDTTXALopMonHocHocVienService.counttblQLDTTXALopMonHocHocVienByCondition(cons)/pageSize + 1; 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&hocVienId="; 
      dataString += "&lopMonHocId="; 
      dataString += "&diemCK="; 
      dataString += "&diemGK="; 
      dataString += "&diemTH="; 
      dataString += "&tongdiem="; 
      dataString += "&trangThai="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_QLDTTXA_LopMonHoc_HocVien Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
